import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This class is Return AssignmentUI
 */
public class RetrunAssignmentUI extends JPanel{
	/**
	 * combo box for course 
	 */
	private JComboBox courseBox = null;
	/**
	 * combo box for assignment
	 */
	private JComboBox assBox = null;
	/**
	 * check button
	 */
	private JButton btnCheck = null;
	/**
	 * back button
	 */
	public JButton btnBack = null;
	/**
	 * title for the current window  
	 */
	private JLabel lbltitle = null;
	/**
	 * help button
	 */
	public JButton btnHelp = null;
	/**
	 * array of assignment
	 */
	private ArrayList<Integer> lass = new ArrayList<Integer>();
	/**
	 * array of course
	 */
	private ArrayList<String[]> lcourse = new ArrayList<String[]>();
	private int courseFlg = 0;
	private Integer assFlg = 1;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 * loaded file
	 */
	private MyFile file = null;
	/**
	 * logout button
	 */
	public JButton btnLogout = null;
	
	public RetrunAssignmentUI (){
		super();
		initializeEmpty();
	}

	private void initializeEmpty() {
		curPanel = this;
		lbltitle = new JLabel("Return Assignment");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);
		
		JLabel lblNoCourses= new JLabel("You don't have any Courses ");
		lblNoCourses.setBounds(135,88, 190, 14);
		this.add(lblNoCourses);
		
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}
/**
 * 
 * @param list
 */
	public RetrunAssignmentUI(Object list) {
		super();
		lcourse = (ArrayList) list;
		initialize();
	}

	private void initialize() {
		curPanel = this;
		
		this.add(getCourseBox(), null);
		this.add(getAssBox(), null);
		setAssBox();
		
		lbltitle = new JLabel("Return Assignment");
		lbltitle.setBounds(180, 28, 110, 14);
		this.add(lbltitle);

		
		
		btnCheck = new JButton();
		btnCheck.setBounds(new Rectangle(130, 180, 195, 25));
		btnCheck.setText("Check Assignments");
		btnCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					CheckAssUI checkAssUI = new CheckAssUI(lcourse.get(courseFlg)[1],assFlg);
					setVisible(false);
					System.out.println("Check Assignments");
					Global.main.setContentPane(checkAssUI);
					checkAssUI.btnBack.addActionListener(new ActionListener() {   //back from defineAssignmentUI
						public void actionPerformed(ActionEvent arg0) {
							Global.main.setContentPane(curPanel);
							curPanel.setVisible(true);
						}
					});	
			}
		});
		this.add(btnCheck);
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		
		btnLogout = new JButton("Logout");     //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}
	
	
	private void setAssBox() {
		assFlg = 1;
		if (assBox!= null)
			assBox.removeAllItems();
		String arr[] = new String[2];
		arr[0] = Global.logIn.getUserlogin();
		arr[1] = lcourse.get(courseFlg)[1];
		SQL s = new SQL(arr,43);
		s.accept();
		Integer size = (Integer)s.getMsg();
		System.out.println("size is :" +size);
			for (int i=0; i<size ; i++)
			{	
				assBox.addItem(i+1);
			}
	}
	
	private JComboBox getCourseBox() {
		if (courseBox == null) {
			courseBox = new JComboBox();
			courseBox.setBounds(new Rectangle(126, 85, 95, 25));  
			int size = lcourse.size();
			String cID;
				for (int i=0; i<size ; i++)
				{
					cID = lcourse.get(i)[1];
					SQL s = new SQL (cID,18);
					s.accept();
					Object msg = s.getMsg();
					System.out.println("this course : " + i + cID); 
					courseBox.addItem((String)msg);
				}
				
			courseBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					courseFlg = courseBox.getSelectedIndex();
					System.out.println("this is : " + courseFlg);
					setAssBox();
				}
			});
		}
		return courseBox;
	}
	
	
	private JComboBox getAssBox() {
		
			assBox = new JComboBox();
			assBox.setBounds(new Rectangle(126, 120, 95, 25));  
			
			assBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					assFlg = assBox.getSelectedIndex();
					assFlg++;
					System.out.println("this is : " + assFlg); 
				}
			});
		
		return assBox;
	}
		
}
	